package com.lyouu.dataminer.factory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.lyouu.dataminer.db.DbConstants;
import com.lyouu.dataminer.util.Configure;

public class MysqlDbFactory {
	public static final String MYSQL_HOST = Configure.getProperty("db_host");
	private static Connection conn = null;

	public static Connection getConnection(String dbName) {
		if (conn == null) {
			try {
				Class.forName(DbConstants.DRIVER).newInstance();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} try {
			conn = DriverManager.getConnection(dbName);
			conn.setAutoCommit(true);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void closeConnection() {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	 
}
